<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>客户类型表单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../../../res/layui/css/layui.css" media="all">
</head>
<script>
    $.ajaxSetup({
        async: false
    });
    $(function(){
        $(".layui-bg-red").click(function(){
            $("#tsone").html("");
            $("#tstwo").html("");
        });

    })
</script>
<body>
<form class="layui-form layui-form-pane" id="formDId" action="" method="post" enctype="multipart/form-data">
    <input type="hidden" name="pkClientTypeId" id ="pkClientTypeId" />

    <div class="layui-form-item">
        <label class="layui-form-label">上级类型</label>
        <div class="layui-input-block">
            <div id="clientTypeSuperior" class="xm-select-demo"></div>
        </div>
        <span id="msg">不选则为一级菜单</span>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">客户类型名称</label>
        <div class="layui-input-block">
            <input type="text" name="clientTypeName" id ="clientTypeName" lay-verify="title" autocomplete="off" placeholder="客户名称" lay-verify="required"
                   class="layui-input">
        </div>
    </div>


    <div class="layui-form-item" id="sub">
        <div class="layui-input-block" id="flows">
            <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1">提交</button>
            <input type="button" id="demo3" class="layui-btn layui-btn-primary" value="取消">
        </div>
    </div>
</form>
<script>

    //需要操作的数据
    let oaClientTypeId = getQueryString("pkClientTypeId");
    //判断是否是查看
    let show = getQueryString("show");
    layui.use(['form', 'laydate', 'table'], function () {
        var table = layui.table
            ,form = layui.form
            , layer = layui.layer;

        //加载组件
        layui.config({
            //引入扩展文件库
            base: getRootPath()+'res/layui/dist/'
        }).extend({
            //引入对应的文件名
            xmSelect: 'xm-select'
            //拿到对应的文件模块
        }).use(['xmSelect'], function(){
            //对于模块赋值使用
            var xmSelect = layui.xmSelect;
            //渲染多选
            pkJurisdictionId = xmSelect.render({
                el: '#clientTypeSuperior',
                radio: true,
                tree: {
                    show: true,
                    strict: false,
                    expandedKeys: [ -1 ],
                },
                height: 'auto',
                data: loadOaClientType(oaClientTypeId,show)
            })

        });
        function loadOaClientType(id,show){
            let url = getRootPath()+"OaClientTypeAction/loadOaClientType";
            let data = {
                id : id, //需要操作的数据
                show:show // 判断是否是查看
            };
            let res = getAjax(url,data);
            if(res){
                return res;
            }else{
                return [];
            }

        }


        let res;
        console.info(oaClientTypeId);
        console.info(show);
        if (oaClientTypeId) {
            res = getAjaxById(getRootPath() + "OaClientTypeAction/findById/" + oaClientTypeId);
            //客户类型id
            $("#pkClientTypeId").val(res.pkClientTypeId);
            //客户类型
            let clientTypeName = $("#clientTypeName");
            clientTypeName.val(res.clientTypeName);

            //判断是否是查看
            if (show) {
                //工作总结类型
                formDisabled($("#pkClientTypeId"));
                //本阶段总结
                formDisabled(clientTypeName);
                formDisabled($("#clientTypeSuperior"));
                console.info("隐藏按钮")
                //隐藏提交按钮
                $("#sub").hide();

            }
            form.render();
        }
        form.render();
        //监听起草提交
        form.on('submit(demo1)', function (data) {

            console.info(data.field);
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            var formData = new FormData(document.getElementById("formDId"));
            console.log(formData)
            let res = postAjaxFile(getRootPath() + "OaClientTypeAction/addOrUpdate", formData);
            if (res.code === 1) {
                layer.msg('新增成功3秒后关闭当前窗口', {icon: 1});
                setTimeout(function () {
                    formClose();
                }, 3000);
                setTimeout(function () {
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }, 3000);
                //window.location.reload();//刷新页面
            } else if (res.code === 500) {
                error('500');
            } else {
                layer.msg("新增失败");
            }
            return false;
        });

        $("#demo3").on("click", function () {
            //询问框
            layer.confirm('是否关闭该页面？', {
                btn: ['是的', '取消'] //按钮
            }, function () {
                formClose();
                parent.layui.table.reload('test');//刷新父页面
                //window.parent.location.reload();//刷新父页面
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });
        });
        var id =getQueryString("pkClientTypeId");
        if(id){
            findById();
        }
    });
    /**
     * 禁用
     *  @param forms 控件
     */
    function formDisabled(forms) {
        const DISABLED = 'layui-btn-disabled';
        forms.attr('disabled', 'disabled');
        forms.addClass(DISABLED);
    }

    function findById() {
        var id =getQueryString("pkClientTypeId");
        var url=getRootPath()+"OaClientTypeAction/findById/"+id;
        var data=getAjaxById(url);
        $("#clientTypeName").val(data.data.clientTypeName);
        $("#pkClientTypeId").val(data.data.pkClientTypeId);
        console.info(data)
    }
</script>

</body>
</html>